package com.showtao.front.controller;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.showtao.common.dto.ResultDto;
import com.showtao.common.dto.ResultDtoFactory;
import com.showtao.common.dto.widget.DataTablesRequestDto;
import com.showtao.common.dto.widget.DataTablesResponseDto;
import com.showtao.common.security.domain.Permission;
import com.showtao.common.security.service.UserService;
import com.showtao.common.validation.ValidateException;
import com.showtao.front.domain.AdminMenu;
import com.showtao.front.domain.SystemLog;
import com.showtao.front.dto.RoleDto;
import com.showtao.front.dto.UserDto;
import com.showtao.front.dto.upstream.AdminMenuSavingDto;
import com.showtao.front.dto.upstream.UserSavingDto;

@Controller
public class UserController {

    @Autowired
    private transient UserService userService; 
    
    @RequiresPermissions (value={"user_admin"})
    @RequestMapping(value = "/admin/users", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<UserDto> doSearchUsers(
            @Valid @RequestBody DataTablesRequestDto requestDto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        Page<UserDto> page = userService.findUsers(pageable);
        DataTablesResponseDto<UserDto> responseDto = new DataTablesResponseDto<UserDto>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"user_admin"})
    @RequestMapping(value = "/admin/user", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doSaveArticle(
            @Valid @RequestBody UserSavingDto dto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        userService.saveUser(dto);
        return ResultDtoFactory.toAck("用户保存成功");
    }
    
    @RequiresPermissions (value={"user_admin"})
    @RequestMapping(value = "/admin/user/{userName}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDto doDeleteArticle(@PathVariable("userName") String userName) throws Exception {
        userService.deleteUser(userName);
        return ResultDtoFactory.toAck("用户删除成功");
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/permissions", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<Permission> doSearchPermissions(
            @Valid @RequestBody DataTablesRequestDto requestDto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        Page<Permission> page = userService.findPermissions(pageable);
        DataTablesResponseDto<Permission> responseDto = new DataTablesResponseDto<Permission>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/menu", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doSaveMenu(
            @Valid @RequestBody AdminMenuSavingDto dto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        userService.saveMenu(dto);
        return ResultDtoFactory.toAck("菜单保存成功");
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/menu/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDto doDeleteMenu(@PathVariable("id") Long id) throws Exception {
        userService.deleteMenu(id);
        return ResultDtoFactory.toAck("菜单删除成功");
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/menus", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<AdminMenu> doSearchMenus(
            @Valid @RequestBody DataTablesRequestDto requestDto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        Page<AdminMenu> page = userService.findAdminMenus(pageable);
        DataTablesResponseDto<AdminMenu> responseDto = new DataTablesResponseDto<AdminMenu>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/roles", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<RoleDto> doSearchRoles(
            @Valid @RequestBody DataTablesRequestDto requestDto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        Page<RoleDto> page = userService.findRoles(pageable);
        DataTablesResponseDto<RoleDto> responseDto = new DataTablesResponseDto<RoleDto>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/logs", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<SystemLog> doSearchLogs(
            @Valid @RequestBody DataTablesRequestDto requestDto,
            BindingResult result) throws Exception {
        // 判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        Page<SystemLog> page = userService.findLogs(pageable);
        DataTablesResponseDto<SystemLog> responseDto = new DataTablesResponseDto<SystemLog>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
}
